/**
 * @description 判断用户是否登录
 */
export function isLogin() {
	const token = uni.getStorageSync('yeyuMini_token');
	return token ? true : false;
}

/**
 * @description 获取用户登录token
 */
export function getToken() {
	return uni.getStorageSync('yeyuMini_token');
}

/**
 * @description 获取用户信息
 */
export function getUserInfo() {
	const userInfo = uni.getStorageSync('yeyuMini_userInfo');
	return userInfo ? JSON.parse(decodeURIComponent(userInfo)) : '';
}

/**
 * @param {Object} phone
 * @description 隐藏手机号中间四位
 */
export function hidePhoneCenter(phone) {
	if (!phone) return; 
	return String(phone).replace(phone.substring(3,7), "****");
}

/**
 * @param {Object} data
 * @description 保存图片到相册
 */
export function saveImageToPhotosAlbum(data = {}) {
	const {
		filePath = '',
		success = () => {},
		fail = () => {},
	} = data;
	
	uni.showModal({
		title: '提示',
		content: '是否保存二维码到相册',
		success: (res) => {
			if (res.confirm) {
				uni.authorize({
					scope: 'scope.writePhotosAlbum',
					success: () => {
						uni.saveImageToPhotosAlbum({
							filePath: filePath,
							success: () => {
								uni.showToast({
									title: '保存成功',
								})
								success();
							},
							fail: () => {
								fail();
							}
						})
					},
					fail: () => {
						uni.showToast({
							icon: 'none',
							title: '获取权限失败',
						})
					}
				})
			}
		}
	});
}